package vashion.azeroth.common.util;

import org.apache.commons.lang3.StringUtils;

/**
 * 商品图片的cdn，后缀等服务 Created by liuhe on 15/10/13.
 */
public class ItemImageUtils {

	private static final String CDN_PREFIX = "http://pic.vashion.me/";
	private static final String OSS_URL = "oss-cn-hangzhou.aliyuncs.com";

	/**
	 * 获取图片完整路径
	 *
	 * @param imgPath
	 * @return
	 */
	public static String getImgFullPath(String imgPath) {
		if (StringUtils.isBlank(imgPath)) {
			return imgPath;
		}
		if (imgPath.contains("aliyuncs") || imgPath.contains("http")) {
			return imgPath;
		}
		return CDN_PREFIX + imgPath;
	}

	/**
	 * 删除图片cdn前缀
	 *
	 * @param imgUrl
	 * @return
	 */
	public static String removeImgCdnPrefix(String imgUrl) {
		if (!StringUtils.isBlank(imgUrl) && imgUrl.startsWith(CDN_PREFIX)
				&& imgUrl.toLowerCase().contains(OSS_URL.toLowerCase())) {
			return imgUrl.substring(CDN_PREFIX.length());
		}
		return imgUrl;
	}
}
